package com.jbl.st.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbl.commonutils.R;
import com.jbl.exceptionhandler.ManagerException;
import com.jbl.st.entity.CourseclassStudent;
import com.jbl.st.entity.Student;
import com.jbl.st.service.CourseclassStudentService;
import com.jbl.st.service.StudentService;
import com.jbl.te.entity.Courseclass;
import com.jbl.te.service.CourseclassService;
import com.jbl.te.service.DepartmentclassService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@RestController
@CrossOrigin
@RequestMapping("/st/courseclass-student")
public class CourseclassStudentController {

    @Autowired
    DepartmentclassService departmentclassService;
    @Autowired
    CourseclassStudentService courseclassStudentService;
    @Autowired
    StudentService studentService;
    @Autowired
    CourseclassService courseclassService;

    @PostMapping("/saveDepartmentclassStudent")
    @ApiOperation("通过行政班id添加学生-课程班中间表")
    @Transactional
    public R saveDepartmentclassStudent(String departmentclassId, String courseclassId) {
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.select("id");
        studentQueryWrapper.eq("departmentclass_id",departmentclassId);
        List<Student> studentList = studentService.list(studentQueryWrapper);
        for (Student student : studentList) {
            //通过行政班所有学生id和课程班id添加进中间表
            CourseclassStudent courseclassStudent = new CourseclassStudent();
            courseclassStudent.setCourseclassId(courseclassId);
            courseclassStudent.setStudentId(student.getId());
            courseclassStudent.setState(true);
            boolean save = courseclassStudentService.save(courseclassStudent);
            if(!save) return R.error().message("中途出现行政班学生添加课程班失败！");
        }
        Courseclass courseclass = courseclassService.getById(courseclassId);
        courseclass.setIsConfirm(true);
        courseclassService.updateById(courseclass);
        return R.ok();
    }

    @DeleteMapping("/delByCourseclassId")
    @ApiOperation("通过课程班id删除所有中间表记录")
    public R delByCourseclassId(String id) {
        QueryWrapper<CourseclassStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("courseclass_id",id);
        boolean remove = courseclassStudentService.remove(wrapper);
        Courseclass courseclass = courseclassService.getById(id);
        courseclass.setIsConfirm(false);
        courseclassService.updateById(courseclass);
        return R.ok();
    }

    @DeleteMapping("/delByCourseclassIdList")
    @ApiOperation("通过课程班id数组删除所有中间表记录")
    @Transactional
    public R delByCourseclassIdList(@RequestBody List<String> idList) {
        for (String id : idList) {
            R r = delByCourseclassId(id);
            if(r.getSuccess() == false) {
                throw new ManagerException(20001,"中途修改失败！");
            }
        }
        return R.ok();
    }

}

